package genetics;

import java.util.Random;

/**
 * Crossover function for a bit gene.
 * @author pcdejager
 */
public class BitCrossoverFunction implements CrossoverFunction {

    /**
     * Implementation of function.
     * @param random The random number generator to use
     * @param parent1 Parent one
     * @param parent2 Parent two
     * @param size The size of the gene
     * @return The resulting gene *
     */
    public final int crossover(final Random random, final int parent1,
            final int parent2, final int size) {
        if (size != 1) {
            return -1;
        }
        if ((parent1 < 0) || (parent2 < 0) || (parent1 > 1) || (parent2 > 1)) {
            return -1;
        }
        if (random.nextBoolean()) {
            return parent1;
        }
        return parent2;
    }
}
